package com.cn.lf.wx.dacs.bussiness.author.utils;

import io.jsonwebtoken.Claims;

import java.util.HashMap;
import java.util.Map;

import com.cn.lf.wx.dacs.bussiness.author.bean.SimpleUser;

/**
 * JSON WEB TOKEN
 * 荷载信息
 * 创建 以及 解析
 */
public class ClaimsUtils {

    public static final String ID = "id";
    public static final String UID = "uid";
    public static final String NAME = "name";
    public static final String AVATAR = "head";
    public static final String EMAIL = "email";
    public static final String SIGN = "sign";
    public static final String STATUS = "status";
    public static final String ROLE = "role";


    public static Map<String, Object> user2Claims(SimpleUser zeroUser) {
        Map<String, Object> claims = new HashMap<>();
        claims.put(ID, zeroUser.getId());
        claims.put(NAME, zeroUser.getName());
        claims.put(UID, zeroUser.getAccount());
        claims.put(AVATAR, zeroUser.getHead());
        claims.put(STATUS, zeroUser.getStatus());
        claims.put(ROLE, zeroUser.getRole());
        return claims;
    }

    public static SimpleUser claims2User(Claims claims) {
        SimpleUser zeroUser = new SimpleUser();
        String id = (String) claims.get(ID);
        zeroUser.setId(id);
        String account = (String) claims.get(UID);
        zeroUser.setAccount(account);
        String name = (String) claims.get(NAME);
        zeroUser.setName(name);
        String avatar = (String) claims.get(AVATAR);
        zeroUser.setHead(avatar);
        String status = (String) claims.get(STATUS);
        zeroUser.setStatus(status);
        String role = (String) claims.get(ROLE);
        zeroUser.setRole(role);
        return zeroUser;
    }
}
